package cn.liu.poi;

import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.*;

import java.io.FileOutputStream;
import java.util.Date;

/**
 * @author LIU
 * @version 1.0
 * @description TODO
 * @date 2023/12/27 10:17
 */
public class ExcelOperTest {
    public static void main(String[] args) throws Exception {
        Workbook workbook = new HSSFWorkbook();
        Sheet sheet = workbook.createSheet("Test");// 创建工作表(Sheet)
        Row row0 = sheet.createRow(0);

        // 设置高宽
        sheet.setColumnWidth(1, 31 * 256);//设置第一列的宽度是31个字符宽度
        row0.setHeightInPoints(50);//设置行的高度是50个点

        // 设置日期格式--使用Excel内嵌的格式
        Cell cell00 = row0.createCell(0);
        cell00.setCellValue(new Date());
        CellStyle style00 = workbook.createCellStyle();
        style00.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy h:mm"));
        cell00.setCellStyle(style00);

        // 单元格对齐
        Cell cell01 = row0.createCell(1);
        cell01.setCellValue("单元格对齐");

        CellStyle style01 = workbook.createCellStyle();
        style01.setAlignment(HorizontalAlignment.CENTER);//水平居中
//        style01.setVerticalAlignment(VerticalAlignment.JUSTIFY);//垂直居中
//        style01.setWrapText(true);//自动换行
//        style01.setIndention((short) 5);//缩进
//        style01.setRotation((short) 60);//文本旋转，这里的取值是从-90到90，而不是0-180度。
        cell01.setCellStyle(style01);


        Cell cell02 =row0.createCell(2);
        cell02.setCellValue("02设置边框");
        CellStyle style02= workbook.createCellStyle();
        style02.setBorderTop(BorderStyle.THIN);//上边框
        style02.setBorderBottom(BorderStyle.THIN);//下边框
        style02.setBorderLeft(BorderStyle.THIN);//左边框
        style02.setBorderRight(BorderStyle.THIN);//右边框
//        style02.setTopBorderColor(HSSFColor.HSSFColorPredefined.GREEN.getIndex());//上边框颜色
//        style02.setBottomBorderColor(HSSFColor.HSSFColorPredefined.GREEN.getIndex());//下边框颜色
//        style02.setLeftBorderColor(HSSFColor.HSSFColorPredefined.PINK.getIndex());//左边框颜色
//        style02.setRightBorderColor(HSSFColor.HSSFColorPredefined.PINK.getIndex());//右边框颜色
        cell02.setCellStyle(style02);


        Cell cell03 = row0.createCell(3);
        cell03.setCellValue("03设置图案样式111");
        CellStyle style03 = workbook.createCellStyle();
//        style03.setFillBackgroundColor(HSSFColor.HSSFColorPredefined.PINK.getIndex());//设置图案背景色
        style03.setFillBackgroundColor((short) 0XF7076D);//设置图案背景色
        style03.setFillForegroundColor((short) 0XF7076D);//设置图案颜色
//        style03.setFillPattern(FillPatternType.SQUARES);//设置图案样式
        cell03.setCellStyle(style03);


        FileOutputStream outputStream = new FileOutputStream("05_poi/03HSSF_test.xls");
        workbook.write(outputStream);
        outputStream.close();
        System.out.println("文档生成完成");
    }
}
